<?php
	session_start();
	
	$whmcs_path="whmcs";	
	require_once("../../../dbconnect.php");
	
	$fileExist = file_exists("utilities/functions.php");	
	
	if($fileExist)		
		include_once "utilities/functions.php";
	else
		exit("<div>Function included was not found</div>");

	
	$shifts = $_POST['staffShift'];
	$month = $_POST['hdnMonth'];
	$year = $_POST['hdnYear'];	
	$user = $_SESSION['adminid'];
	
	$length = count($shifts);
	$i=1;
	
	$isNewRecord = isNewShift($user, $month, $year);
	
	if($isNewRecord){
		$query = "INSERT INTO mod_shift(userid, month, year, records)
				values('".$user."', '".$month."', '".$year."','";
				
		$length = count($shifts);
		$i=1;
		foreach ($shifts as $item => $key){
			if($i!=$length)
				$query .= $key.",";
			else
				$query .= $key."')";
			$i++;
		}		
		
		$rs = mysql_query($query);
	}
	else{
		$query = "UPDATE mod_shift SET records = '";		
		foreach ($shifts as $item => $key){
			if($i!=$length)
				$query .= $key.",";
			else
				$query .= $key."'";
			$i++;
		}
		if(count($shifts)==0)
			$query .= "' WHERE userid = ".$user;
		else
			$query .= " WHERE userid = ".$user;
		
				
		$update = mysql_query($query);
		
		/*if($update)
			echo "Updated";
		else
			echo "Error with SQL Statement: ".$query;*/
	}

	
	$path = "http://" . $_SERVER['SERVER_NAME'] . "/whmcs/admin/addonmodules.php?module=shift_manager";
	
	if($month!=date('n') || $year!=date('Y'))
		$path = "http://" . $_SERVER['SERVER_NAME'] . "/whmcs/admin/addonmodules.php?module=shift_manager&month=".$month."&year=".$year;
		
	header("location: ".$path);


	
?>
